const App = getApp();
const func = require("../../utils/func.js");
const util = require("../../utils/util.js")
Page({

	/**
	 * 页面的初始数据
	 */
	data: {
		typeid: 0,
		keyword: '', //搜索关键词
		list: {}, //商品列表
		dropdownList: [{
				name: '综合',
				selected: true,
				orderway: '',
				orderby: ''
			},
			{
				name: '价格升序',
				selected: false,
				orderway: 'asc',
				orderby: 'users_price'
			},
			{
				name: '价格降序',
				selected: false,
				orderway: 'desc',
				orderby: 'users_price'
			}
		],
		selectedName: '综合',
		selectH: 0,
		orderIndex: 0,
		orderby: '',
		orderway: '',
		apiChannel: [],
		min_users_price: '', //最低价
		max_users_price: '', //最高价

		width: 200, //header宽度
		height: 64, //header高度
		inputTop: 0, //搜索框距离顶部距离
		arrowTop: 0, //箭头距离顶部距离
		dropScreenH: 0, //下拉筛选框距顶部距离
		attrData: [],
		attrIndex: -1,
		dropScreenShow: false,
		scrollTop: 0,
		tabIndex: 0, //顶部筛选索引
		isList: false, //是否以列表展示  | 列表或大图
		drawer: false,
		drawerH: 0, //抽屉内部scrollview高度
		goodCount: 0,
		page: 1,
		isLoading: true,
		no_more: false,
		show:true,
	},

	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: function(options) {
		let _this = this,
			obj = wx.getMenuButtonBoundingClientRect();
		_this.setData({
			width: obj.left,
			height: obj.top + obj.height + 8,
			inputTop: obj.top + (obj.height - 30) / 2,
			arrowTop: obj.top + (obj.height - 32) / 2,
			keyword: options.keyword || "",
			typeid: options.type_id || '',
			orderway:options.orderway || '',
			orderby:options.orderby || '',
			aids:options.aids || '',
			keyword_text: options.text || ''
		}, () => {
			wx.getSystemInfo({
				success: (res) => {
					_this.setData({
						//略小，避免误差带来的影响
						wHeight:res.screenHeight-obj.top - obj.height -50,
						dropScreenH: _this.data.height * 750 / res.windowWidth +
							86,
						drawerH: res.windowHeight - res.windowWidth / 750 *
							100 - _this.data.height
					})
				}
			})
		});
		_this.getPageData();
		_this.getNavigation();
	},
	/**
	 * 加载分类数据
	 */
	getNavigation: function() {
		let _this = this;
		App._requestApi(_this, App.globalData.config.apiIndexUrl, {
			apiChannel_1: `ekey=1&type=top&channelid=2`,
		}, function(res) {
			let apiChannel = res.data.apiChannel[1].data;
			_this.setData({
				apiChannel,
			});
		});
	},
	showup() {
		let _this = this;
		_this.setData({
			show:!_this.data.show
		})
	},
	/**
	 * 加载页面数据
	 */
	getPageData(isPage, page) {
		let _this = this,
			arr = [{
					field: "keyword",
					value: _this.data.keyword
				},
				{
					field: "typeid",
					value: _this.data.typeid
				},
				{
					field: "aids",
					value: _this.data.aids
				},
				{
					field: "orderby",
					value: _this.data.orderby
				},
				{
					field: "orderway",
					value: _this.data.orderway
				},
				{
					field: "min_users_price",
					value: _this.data.min_users_price
				},
				{
					field: "max_users_price",
					value: _this.data.max_users_price
				}
			];
		page = page || 1;
		let field = `ekey=1&seckill=on&pagesize=6&page=` + page;
		arr.forEach(function(v, index) {
			if (v.value != '') {
				field += `&` + v.field + `=` + v.value;
			}
		});
		App._requestApi(_this, App.globalData.config.apiIndexUrl, {
			apiList_1: field,
		}, function(res) {
			let list = res.data.apiList[1]; // 第一个list文档列表数据
			if (isPage == true) {
				let dataList = _this.data.list;
				_this.setData({
					'list.data': dataList.data.concat(list.data),
					isLoading: false
				})
			} else {
				_this.setData({
					list,
					isLoading: false
				})
			}
			let goodCount = 0;
			if (list.total < 100) {
				goodCount = list.total;
			} else if (list.total > 100 && list.total < 1000) {
				goodCount = Math.floor(list.total / 100) * 100 + '+';
			} else if (list.total > 1000 && list.total < 10000) {
				goodCount = Math.floor(list.total / 1000) * 1000 + '+';
			} else {
				goodCount = Math.floor(list.total / 10000) + '万+';
			}
			_this.setData({
				goodCount
			})
		});
	},
	delete(e) {
		let _this = this;
		_this.setData({
			keyword:'',
			keyword_text:''
		})
		_this.onPullDownRefresh()
		
	},
	dropdownItem(e) {
		let _this = this,
			index = e.currentTarget.dataset.index,
			dropdownList = _this.data.dropdownList;
		_this.setData({
			orderIndex: index,
			selectedName: dropdownList[index].name,
			orderby: dropdownList[index].orderby,
			orderway: dropdownList[index].orderway
		});
		_this.data.page = 1;
		_this.data.list.last_page = 1;
		_this.setData({ isLoading: true });
		_this.getPageData();
		_this.hideDropdownList();
	},
	// 顶部筛选栏
	screen: function(e) {
		let _this = this,
			index = e.currentTarget.dataset.index;
		_this.hideDropdownList();
		if (index == 0) {
			_this.showDropdownList();
		} else if (index == 1) {
			_this.setData({
				tabIndex: 1,
				isLoading: true,
				orderby: 'total_sales_num',
				orderway: 'desc'
			});
			_this.data.page = 1;
			_this.data.list.last_page = 1;
			_this.getPageData();
		} else if (index == 2) {
			_this.setData({
				isList: !_this.data.isList
			})
		} else if (index == 3) {
			_this.setData({
				drawer: true
			})
		}
	},
	// 隐藏弹窗列表
	hideDropdownList: function() {
		this.setData({
			selectH: 0
		})
	},
	showDropdownList: function() {
		this.setData({
			selectH: 246,
			tabIndex: 0
		})
	},
	changeType(e) {
		let _this = this,
			index = e.currentTarget.dataset.index;
		_this.setData({
			typeid: _this.data.apiChannel[index].typeid,
		});
		_this.data.page = 1;
		_this.data.list.last_page = 1;
		_this.setData({ isLoading: true });
		_this.getPageData();
	},
	inputMin(e) {
		let _this = this,
			value = e.detail.value
		_this.setData({
			min_users_price: util.trim(value)
		})
		_this.data.page = 1;
		_this.data.list.last_page = 1;
		_this.setData({ isLoading: true });
		_this.getPageData();
	},
	inputMax(e) {
		let _this = this,
			value = e.detail.value
		_this.setData({
			max_users_price: util.trim(value)
		})
		_this.data.page = 1;
		_this.data.list.last_page = 1;
		_this.setData({ isLoading: true });
		_this.getPageData();
	},
	resetDrawer() {
		let _this = this;
		_this.setData({
			max_users_price: '',
			min_users_price: '',
			typeid: ''
		})
		_this.data.page = 1;
		_this.data.list.last_page = 1;
		_this.setData({ isLoading: true });
		_this.getPageData();
	},
	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow() {

	},
	// 跳转搜索界面
	jumpToSearch() {
		wx.navigateTo({
			url: `/pages/search/index`
		});
	},

	// 关闭左部弹窗
	closeDrawer: function() {
		this.setData({
			drawer: false
		})
	},

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh() {
		let _this = this;
		_this.data.page = 1;
		_this.data.list.last_page = 1;
		_this.setData({ isLoading: true });
		this.getPageData(); // 获取首页数据
		wx.stopPullDownRefresh(); // 停止下拉刷新
	},

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom() {
		let _this = this;
		if (_this.data.page >= _this.data.list.last_page) {
			_this.setData({
				no_more: true
			});
			return false;
		}
		_this.getPageData(true, ++_this.data.page);
	},
	back: function() {
		if (this.data.drawer) {
			this.closeDrawer();
		} else {
			wx.navigateBack();
		}
	},
	/**
	 * 分享当前页面
	 */
	onShareAppMessage() {
		let _this = this;
		return {
			title: _this.data.goodsData.title,
			path: "/pages/product/list?" + App.getShareUrlParams()
		};
	},
	/**
	 * 分享到朋友圈
	 */
	onShareTimeline() {
		let _this = this;
		return {
			title: _this.data.goodsData.title,
		}
	},
	jumpToView(e) {
		func.jumpToUrl(e)
	}
})
